package com.moxiao.yfjh.module.blind.service.basicSetting;

import com.moxiao.yfjh.module.blind.common.CommonRedisUtils;
import com.moxiao.yfjh.module.blind.controller.admin.basicSetting.vo.BlindBasicSettingCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.basicSetting.vo.BlindBasicSettingExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.basicSetting.vo.BlindBasicSettingPageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.basicSetting.vo.BlindBasicSettingUpdateReqVO;
import com.moxiao.yfjh.module.blind.convert.basicSetting.BlindBasicSettingConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.basicSetting.BlindBasicSettingDO;
import com.moxiao.yfjh.module.blind.dal.mysql.basicSetting.BlindBasicSettingMapper;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.blind.controller.admin.basicSetting.vo.*;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲基础设置 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class BlindBasicSettingServiceImpl implements BlindBasicSettingService {

    @Resource
    private BlindBasicSettingMapper basicSettingMapper;

    @Resource // 保证 aj-captcha 的 SPI 创建时的注入
    private CommonRedisUtils commonRedisUtils;

    @Override
    public Long createBasicSetting(BlindBasicSettingCreateReqVO createReqVO) {
        // 插入
        BlindBasicSettingDO basicSetting = BlindBasicSettingConvert.INSTANCE.convert(createReqVO);
        basicSettingMapper.insert(basicSetting);
        // 返回
        Long id = basicSetting.getId();
        if (Objects.nonNull(id)){
            commonRedisUtils.createBasicToRedis(id,basicSetting);
        }
        return id;
    }

    @Override
    public void updateBasicSetting(BlindBasicSettingUpdateReqVO updateReqVO) {
        // 校验存在
        validateBasicSettingExists(updateReqVO.getId());
        // 更新
        BlindBasicSettingDO updateObj = BlindBasicSettingConvert.INSTANCE.convert(updateReqVO);
        basicSettingMapper.updateById(updateObj);

        commonRedisUtils.delBasicToRedis(updateReqVO.getId());
        BlindBasicSettingDO basicSetting = getBasicSetting(updateReqVO.getId());
        if (Objects.nonNull(basicSetting)){
            commonRedisUtils.createBasicToRedis(updateReqVO.getId(),basicSetting);
        }
    }

    @Override
    public void deleteBasicSetting(Long id) {
        // 校验存在
        validateBasicSettingExists(id);
        // 删除
        basicSettingMapper.deleteById(id);
        commonRedisUtils.delBasicToRedis(id);
    }

    private void validateBasicSettingExists(Long id) {
        if (basicSettingMapper.selectById(id) == null) {
            throw exception(BASIC_SETTING_NOT_EXISTS);
        }
    }


    @Override
    public BlindBasicSettingDO getBasicSetting(Long id) {
        BlindBasicSettingDO basicToRedis = commonRedisUtils.getBasicToRedis(id);
        if (Objects.nonNull(basicToRedis)){
            return basicToRedis;
        }
        return basicSettingMapper.selectById(id);
    }

    @Override
    public List<BlindBasicSettingDO> getBasicSettingList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return basicSettingMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BlindBasicSettingDO> getBasicSettingPage(BlindBasicSettingPageReqVO pageReqVO) {
        return basicSettingMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BlindBasicSettingDO> getBasicSettingList(BlindBasicSettingExportReqVO exportReqVO) {
        return basicSettingMapper.selectList(exportReqVO);
    }

}
